import md5 from 'js-md5'
import axios from 'axios'
let uploadFile = function(url, header, pathPath,name) {
	return new Promise((a, b) => {
		uni.uploadFile({
			url,
			filePath,
			name,
			header,
			success: up =>{
				console.log("调用uploadFile的结果是:",up)
				a(up)
			},
			fail:f=> {
				b(f)
			}
		})
	})
}

let request = function(url, head, method, data) {
	console.log("url:",url);
	console.log("head:",head);
	console.log("method:",method);
	console.log("data:",data);
	return new Promise((a, b) => {
		uni.request({
			url: url,
			header: head == null ? {
				'X-Requested-With': 'XMLHttpRequest',
				"Accept": "application/json",
				"Content-Type": "application/json; charset=UTF-8"
			} : head,
			method: method,
			data: data,
			success: function(e) {
				a(e)
			},
			fail: function(e) {
				b(e)
			}
		})
	})
}

let verificationMsg = function(url, p, t) {
	//短信验证码
	let phone = p;
	let type = t;
	return new Promise((a, b) => {
		uni.request({
			url: url,
			method: "POST",
			header: {
				"Accept": "application/vnd.fy.v1+json",
				"Content-Type": "application/json",
			},
			data: {
				phone,
				type
			},
			success: function(e) {
				a(e)
			},
			fail: function(e) {
				b(e)
			}
		})
	})
}

let login = function(H5Openid, MPOpenid, unionID) {
	//小程序和公众号登陆方法，传入openid或者是
	let http = getApp().globalData.http
	let sendData = ""

	//#ifdef H5
	//以下代码只会在H5运行
	if (!H5Openid) {
		console.error("调用登陆接口请传入公众号CODE")
	}
	sendData = {
		openid_gzh: H5Openid,
		openid_xcx: "",
		unionid: unionID || ''
	}
	//#endif
	//#ifdef MP-WEIXIN
	//以下代码只会在微信小程序里面运行
	if (!MPOpenid) {
		console.error("调用登陆接口请传入小程序CODE")
	}
	sendData = {
		openid_gzh: "",
		openid_xcx: MPOpenid,
		unionid: unionID || ''
	}
	//#endif
	console.log("调用登陆时发送的数据是:",sendData);
	return new Promise((a, b) => {
		uni.request({
			url: `${http}api/pigfy/login`,
			method: "POST",
			header: {
				"Accept": "application/vnd.fy.v1+json",
				"Content-Type": "application/json"
			},
			data: sendData,
			success: e => {
				a(e)
			},
			fail: e => {
				b(e)
			}
		})
	})

}

let getWEIXINCode = function() {
	//这个方法只能在小程序里面用
	return new Promise((a, b) => {
		uni.login({
			success: e => {
				a(e)
			},
			fail: e => {
				b(e)
			}
		})
	})
}

// http://112.124.57.249:9090/resHttp
const mySecret = "f6fdffe48c908deb0f4c3bd36c032e72"
// const baseURL= 'https://live.xstrive.com/85bf64d66a465343c5fb9562aa6946db'
const baseURL= 'https://www.gdruiduo.com/res'
// const baseURL= 'http://112.124.57.249:9090'
export const md5Params = (params={}) => {
	params.t = (new Date().getTime() / 1000).toFixed(0)
	let str = "";
	let keys = Object.keys(params);
	keys.push("secret");
	keys = keys.sort(); // 对参数 key 进行排序
	keys.forEach((key) => {
		if (key == "token") {
			return;
		}
		if (str != "") {
			str += "&";
		}
		if (key == "secret") {
			str += `${key}=${mySecret}`;
		} else {
			str += `${key}=${params[key]}`;
		}
	});
	params.token = md5(str);
	
	return params;
};

const axiosInstance = axios.create({
	baseURL
});

axiosInstance.interceptors.request.use(
	config => {
		console.log('params>>>',config)
		// 也可以在这里添加其他参数
		config.params = md5Params(config.params);
		return config;
	},
	error => {
		// 对请求错误做些什么
		return Promise.reject(error);
	}
);


export {
	axiosInstance,
	verificationMsg,
	request,
	getWEIXINCode,
	login, 
	uploadFile
}
